package com.ruixun.shopmanagement.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruixun.shopmanagement.dao.OrderItemMapper;
import com.ruixun.shopmanagement.dao.OrdersMapper;
import com.ruixun.shopmanagement.entity.Order;
import com.ruixun.shopmanagement.entity.OrderItem;
import com.ruixun.shopmanagement.entity.Orders;
import com.ruixun.shopmanagement.entity.Page;
import com.ruixun.shopmanagement.entity.Page2;
import com.ruixun.shopmanagement.entity.Product;
import com.ruixun.shopmanagement.service.inter.OrderService;
@Service
public class OrderSerivceImpl implements OrderService {
	@Autowired
	OrdersMapper ordersMapper;
	@Autowired
	OrderItemMapper orderItemMapper;
	
	@Transactional
	@Override
	public boolean save(Orders order, List<OrderItem> orderItemList) {
		// TODO Auto-generated method stub
		//保存订单
		if(!ordersMapper.insertOrder(order)) {
			return false;
		}
		
		//保存订单项
		for (OrderItem orderItem:orderItemList) {
			if(!orderItemMapper.insertOrderItem(orderItem)) { //假如添加失败返回false
				return false;
			}
			
		} 
		return true;
	}

	@Override
	public Page2 findPage2ByUid(String uid, Integer targetPage,boolean asc,Integer pagesize2) {
	
		Page2 page=new Page2();
		page.setPageSize(pagesize2);
		Integer pagesize=page.getPageSize();
		List<Order> list=ordersMapper.selectOrderAndOrderItemByUid(uid, (targetPage-1)*pagesize,pagesize,asc );
		page.setList(list);
		page.setTotalCount(ordersMapper.selectTotalOrderByUid(uid));
		page.setCurrPage(targetPage);
		int totalPage=page.getTotalCount()%page.getPageSize()==0?page.getTotalCount()/page.getPageSize():page.getTotalCount()/page.getPageSize()+1;
		page.setTotalPage(totalPage);
		return page;
	}

	
}
